# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=py3-twisted
_pkgname=Twisted
pkgver=20.3.0
pkgrel=1
pkgdesc="Asynchronous networking framework written in Python3"
url="http://twistedmatrix.com/"
arch="all"
license="MIT"
depends="python3 py3-cryptography py3-zope-interface py3-constantly py3-incremental py3-attrs
	py3-pyhamcrest py3-hyperlink py3-automat"
makedepends="libtirpc-dev py3-setuptools python3-dev cython"
checkdepends="xvfb-run py3-appdirs tzdata py3-asn1 py3-bcrypt"
subpackages="$pkgname-doc"
source="https://twistedmatrix.com/Releases/$_pkgname/${pkgver%.*}/$_pkgname-$pkgver.tar.bz2
	remove-locale-dependent-tests.patch
	disable-failing-test.patch
	disable-test-missing-ckeygen.patch
	disable-errored-tests.patch
	"
builddir="$srcdir"/"$_pkgname"-$pkgver

replaces="py-twisted" # Backwards compatibility
provides="py-twisted=$pkgver-r$pkgrel" # Backwards compatibility

# secfixes:
#   20.3.0-r0:
#     - CVE-2020-10108
#     - CVE-2020-10109
#   19.10.0-r0:
#     - CVE-2019-9512
#     - CVE-2019-9514
#     - CVE-2019-9515
#   19.7.0-r0:
#     - CVE-2019-12387
#     - CVE-2019-12855
#   16.4.0-r0:
#     - CVE-2016-1000111

prepare() {
	default_prepare
	# Remove tests that always fail due to our not-upstream testing procedure
	# these tests if invoking the Twisted binary and twisted.trial tests work
	rm -f src/twisted/test/test_main.py
	rm $(grep -lr "Generated by Cython" .)
}

build() {
	find -name '*.pyx' -exec cython {} \;
	CFLAGS="$CFLAGS $(pkgconf --cflags libtirpc)" python3 setup.py build
}

check() {
	PYTHONPATH="$(echo $PWD/build/lib.*)" xvfb-run python3 \
		-c 'from twisted.scripts.trial import run; run()' twisted
}

package() {
	python3 setup.py install --prefix=/usr --root="$pkgdir"
	# Do not remove the test as it is a legitimate module and not
	# a (common) packaging mistake by upstream
	# rm -rf "$pkgdir"/usr/lib/python3*/site-packages/twisted/test

	_mans="pyhtmlizer.1 trial.1 twistd.1"
	for _man in $_mans; do
		install -m644 -D "$builddir"/docs/core/man/$_man \
			"$pkgdir"/usr/share/man/man1/$_man
	done
}

doc() {
	replaces="py-twisted-doc" # Backwards compatibility
	default_doc
}

sha512sums="1b850e5fc21a3630ead4c2cc3622c16e78bb3be38ab11d021779b7ce3d3c30acc4e19d79c7791a5fce6c5c6e09c2baa349901dffe952de67dd98eec419846365  Twisted-20.3.0.tar.bz2
b8532d6ad572c7f13cddce35e2aa03d28b7e2b22ace7976e92e617aa26f15ea518f8cbd5efd560f841a585356323815d39257a49b39c9caae505ceff44c4435c  remove-locale-dependent-tests.patch
a20bd2e013895adac17334b32e336f7d8b81f6a8f4523c2b76e21bd1cc852f29251ba90f2c93caa89ca4a14b1b9639da7cf54b5effc18b77f5f64430907f9088  disable-failing-test.patch
faace475ed4a051352fb1f9eee96565df0b592286220698f2e13150a484d2a1e0b7f588b1e3ef08eb4b7604083df3b723a1997353e767e0a56e9dedbb3af45b3  disable-test-missing-ckeygen.patch
1660a7966ec1936378d623c279a8660822b65356838dbcd411f53215d16bcfe4d39518c3e98dbcea1937b4ab3eabb143a3ced299d6719ae33101074eebda71fc  disable-errored-tests.patch"
